package lero.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * 作者: lero
 * 日期：2018/7/11
 */

@Aspect
@Component
public class LogAop {
    //定义一个全局的日志记录器，通过LoggerFactory获取
    private static final Logger logger = LoggerFactory.getLogger(LogAop.class);

    //execution(* org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(..))
    @Around("execution(* lero.controller.*.*(..))")
    public Object  recordParam(ProceedingJoinPoint joinPoint){
        Object result = null;
        String methodName = joinPoint.getSignature().toLongString();
        try {
            //前置通知
            Object[] args = joinPoint.getArgs();
            logger.debug("【lero】==>进入方法{},入参{}",methodName,Arrays.toString(args));
            //执行目标方法
            result = joinPoint.proceed();
            //后置通知
            logger.debug("【lero】==>执行方法{},返回{}",methodName,result);
        } catch (Throwable throwable) {
            //异常通知
            logger.debug("【lero】==>异常方法{},异常{}",methodName,throwable.getMessage());
            throwable.printStackTrace();
        }finally {
            //最终通知
        }
        return  result;
    }
}
